<?php
import("com.miho.io.File");
import("com.miho.config.ConfigFileNotFoundException");
import("com.miho.logger.Logger");
Logger::getLogger("Config")->setLevel(Logger::ERROR_LEVEL);
class Config {
	private static $_instances=array();
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $path
	 * @return Config
	 */
	public static function getConfig($path){
		if(!isset(self::$_instances[$path])){
			self::$_instances[$path] = new Config($path);
		}
		return self::$_instances[$path];
	}
	
	/**
	 * Enter description here...
	 *
	 * @var DOMDocument
	 */
	private $_dom;
	private $_xpath;
	private function __construct($path){
		if (!File::exists($path)) {
			throw new ConfigFileNotFoundException($path); 
		}
		$this->_dom = new DOMDocument();
		$this->_dom->preserveWhiteSpace = false;
		if (!$this->_dom->load($path)){
			throw new ConfigFileNotFoundException($path);
		}
		$this->_xpath = new DOMXPath($this->_dom);
	}
	
	public function get($uri,$default=null){
		$query="//".str_replace(".","/",$uri);
		$result =$this->_xpath->query($query); 
		if($result->length==1){
			return $result->item(0)->nodeValue;
		}
		return $default;
	}
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $uri
	 * @return DOMNodeList
	 */
	public function getElement($uri){
		$query="//".str_replace(".","/",$uri);
		$result =$this->_xpath->query($query); 
		Logger::getLogger("Config")->log("result length ".$result->length);
		return $result;
	}
}

?>